/*
 * Copyright 2006-2010 Edward L. Stauff.                  Contact: <EdStauff@gmail.com>.
 * 
 * This file, "AppExitCode.cs", is part of the "Stauffware.Common" class library.
 * "Stauffware.Common" is free software: you can redistribute it and/or 
 * modify it under the terms of the GNU Lesser Public License as published by the 
 * Free Software Foundation, either version 3 of the License, or (at your option) 
 * any later version.
 * 
 * "Stauffware.Common" is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser Public License for more 
 * details.
 * 
 * You should have received a copy of the GNU Lesser Public License along with 
 * the "Stauffware.Common" sources.  If not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.Collections.Generic;
using System.Text;
using Stauffware.Common.Core;

namespace Stauffware.Common.Console
{
	/// <summary>
	/// An exit code for an application (usually a console app).
	/// </summary>
	public class AppExitCode
	{
		/// <summary>
		/// The integer value for the exit code; what gets returned by the application.
		/// </summary>
		/// <remarks>
		/// By convention, a value of 0 usually indicates success.
		/// After the application exits, this value is available from 
		/// System.Diagnostics.Process.Exit and the ERRORLEVEL environment variable.
		/// </remarks>
		public readonly int Value;

		/// <summary>
		/// Indicates whether the exit code indicates an error.
		/// </summary>
		public readonly bool IsError;

		/// <summary>
		/// A description of the exit code.
		/// </summary>
		public readonly string Description;

		/// <summary>
		/// A list of all the exit code objects that have been instantiated.
		/// </summary>
		public static readonly SortedList<int, AppExitCode> AllExitCodes = 
										new SortedList<int, AppExitCode>();

		/// <summary>
		/// constructor
		/// </summary>
		public AppExitCode (int value, bool isError, string description)
		{
			Require.Condition(!AllExitCodes.ContainsKey(value), 
				"AppExitCode already defined for value " + value.ToString());

			this.Value = value;
			this.IsError = isError;
			this.Description = description;

			AllExitCodes.Add(value, this);
		}

		///// <summary>
		///// Exits the application and returns this error code.
		///// </summary>
		//public void Return ()
		//{
		//    Environment.Exit(this.Value);
		//}

		///// <summary>
		///// Displays this error code's description, exits the application,
		///// and returns this error code.
		///// </summary>
		//public void DisplayAndReturn ()
		//{
		//    if (this.IsError)
		//        System.Console.Error.WriteLine("*** ERROR: " + this.Description);
		//    else System.Console.WriteLine(this.Description);

		//    Environment.Exit(this.Value);
		//}
	}
}
